package com.njit.mailm.core.controller;

import com.njit.mailm.commmon.constant.MailConstant;
import com.njit.mailm.commmon.utils.GUID;
import com.njit.mailm.core.bean.Classify;
import com.njit.mailm.core.bean.User;
import com.njit.mailm.core.service.TreeService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2016/5/9.
 */
@RequestMapping(value = "/tree")
@Controller
public class TreeController {

    @Autowired
    private TreeService treeService;

    @ResponseBody
    @RequestMapping(value = "ajaxAddRoot",method = RequestMethod.POST)
    public boolean ajaxAddRoot(Classify classify, HttpSession session){
        classify.setId(GUID.getGUID());
        classify.setUser((User) session.getAttribute(MailConstant.CURRENTUSER));
        classify.setRoot(true);
        return treeService.addRoot(classify);
    }

    @ResponseBody
    @RequestMapping(value = "ajaxUpdateRoot",method = RequestMethod.POST)
    public boolean ajaxUpdateRoot(String origin,String newName,HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        Classify classify = treeService.getClassifyByName(origin,user.getId());

        return treeService.updateRoot(classify,newName);
    }
    @ResponseBody
    @RequestMapping(value = "ajaxDeleteRoot",method = RequestMethod.POST)
    public boolean ajaxDeleteRoot(String name,HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        Classify classify = treeService.getClassifyByName(name,user.getId());

        return treeService.deleteRoot(classify);
    }
    @ResponseBody
    @RequestMapping(value = "addChildElement",method = RequestMethod.POST)
    public boolean addChildElement(String name,String parentName,HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        return treeService.addChildElement(name,parentName,user);
    }
    @ResponseBody
    @RequestMapping(value = "updateChildElement",method = RequestMethod.POST)
    public boolean updateChildElement(String name,String originName,HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        return treeService.updateChildElement(name,originName,user);
    }

    @ResponseBody
    @RequestMapping(value = "deleteChildElement",method = RequestMethod.POST)
    public boolean deleteChildElement(String name,HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        return treeService.deleteChildElement(name,user);
    }

    @ResponseBody
    @RequestMapping(value = "listAllElement" ,method = RequestMethod.GET)
    public List<Classify> listAllElement(HttpSession session){
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        List<Classify> result = treeService.listAllElement(user);
        return result;
    }

}
